/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.rest;

import com.mycompany.delivery.dto.ContactDTO;
import com.mycompany.delivery.service.AccountServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.mycompany.delivery.servicelayer.ContactService;
import com.mycompany.delivery.web.EmployeeController;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Mato
 */
@RestController
@RequestMapping("/contacts")
public class ContactRestController {

    final static Logger log = LoggerFactory.getLogger(ContactRestController.class);

    @Autowired
    private ContactService contactService;

    @Autowired
    @Qualifier("accUserDetailsService")
    private AccountServiceImpl accUserDetailsService;

    public void LoginCheck() {
         Authentication auth = SecurityContextHolder.getContext().getAuthentication();
         if(auth == null || !"rest".equals(auth.getName())) {
              UserDetails userDetails = accUserDetailsService.loadUserByUsername("rest");
            Authentication authentication = new UsernamePasswordAuthenticationToken(
                    userDetails, userDetails.getPassword(), userDetails.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authentication);
            log.debug("rest loged");
         }else {
             log.debug("loged");
         }
    }

    @RequestMapping(value = "/getContacts", headers = "Accept=text/plain")
    public String getContacts() {
        LoginCheck();
        String contacts = "";

        for (ContactDTO c : contactService.getAllContacts()) {
            contacts += " " + c;
        }

        return contacts;
    }

    @RequestMapping(value = "/getContact/{id}", method = RequestMethod.GET, headers = "Accept=text/plain")
    public String getContact(@PathVariable("id") Long id) {
        LoginCheck();
        return (contactService.find(id)).toString();
    }

    @RequestMapping(value = "/getContactJson/{id}", method = RequestMethod.GET, headers = "Accept=application/json; charset=UTF-8")
    public ContactDTO getContactAsJSON(@PathVariable("id") Long id) {
        LoginCheck();
        return contactService.find(id);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ContactDTO update(@RequestBody ContactDTO contact) {
        LoginCheck();
        ContactDTO returnContact;
        if (contact.getId() == null || contact.getId() == 0) {
            returnContact = contactService.create(contact);
        } else {
            returnContact = contactService.update(contact);
        }
        return returnContact;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ContactDTO delete(@RequestBody ContactDTO contact) {
        LoginCheck();
        return contactService.remove(contact);
    }

    @RequestMapping(value = "/getAllContactJson", method = RequestMethod.GET, headers = "Accept=application/json; charset=UTF-8")
    public List<ContactDTO> getContactAsJSON() {
        LoginCheck();
        return contactService.getAllContacts();
    }

}
